View Javadoc
1 /* 2 * Title: S/MIME Project 3 * Description: S/MIME email sending capabilities 4 * @Author Vladan Obradovic 5 * @Version 2.0.1 6 */ 7 8 package org.webdocwf.util.smime.test; 9 10 11 import javax.mail.Transport; 12 import org.webdocwf.util.smime.smime.SignedAndEnvelopedSMIME; 13 import org.webdocwf.util.smime.exception.SMIMEException; 14 import java.io.FileInputStream; 15 import java.io.File; 16 17 18 /*** 19 * Tests signing and enveloping process. Signed and enveloped 20 * text/html message with or withouth attachments can be sent by this test. 21 * This example is test for composing of email message content by html code which 22 * is given from the file system. To get help for this example type: 23 * "java org.webdocwf.util.smime.test.TestSigEncHtml" in command line. 24 * It is assumed that smime200tests.jar is in your classpath.<BR> 25 * <BR> 26 * Parameters passed to example are:<BR> 27 * <mailHost> <mailAddress> <cerFileName> <algorithmName> 28 * <digestAlgorithm> <includingCert> <includingSignAttrib> 29 * <pfxFileName> [<attachment>] <BR> 30 * <BR> 31 * <digestAlgorithm> could be: SHA1_WITH_RSA, MD2_WITH_RSA, MD5_WITH_RSA or SHA1_WITH_DSA.<BR> 32 * <includingCert> could be: true/false<BR> 33 * <includingSignAttrib> could be: true/false<BR> 34 * <algorithmName> could be: RC240, RC264, RC2128, 3DES or 3DES<BR> 35 * <BR> 36 * Note that for this example passwords for .pfx or .p12 files are fixed to 37 * "sea1". All .pfx files or .p12 files provided with this example have this 38 * password. Also, email address "FROM" is fixed to: "sender@seateam.co.yu". 39 * You should change this values in source code of TestSigEncHtml.java in order 40 * to use them with other .pfx or .p12 files and corresponding "FROM" addresses and passwords. 41 */ 42 public class TestSigEncHtml { 43 44 public static void main(String[] args) { 45 46 String subject = "S/MIME signed and enveloped message - Subject test: ÜüÄäÖöÜüß"; 47 String content = "S/MIME signed and enveloped message example\r\nContent test: ÜüÄäÖöÜüß!"; 48 String from = "sender@seateam.co.yu"; 49 String password = "sea1"; 50 51 if (args.length < 8) { 52 System.err.println( 53 System.getProperty("line.separator") + 54 "Usage of TestSigEncHtml: " + 55 System.getProperty("line.separator") + 56 "java TestSigEncHtml <mailHost> <mailAddress> <cerFileName> " + 57 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 58 "<pfxFileName> [<attachment>]" + 59 System.getProperty("line.separator") + 60 System.getProperty("line.separator") + 61 "Examples:" + 62 System.getProperty("line.separator") + 63 "java TestSigEncHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 64 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 65 System.getProperty("line.separator") + 66 "java TestSigEncHtml seateam.co.yu recipient@seateam.co.yu recipient512.cer " + 67 "DES MD5_WITH_RSA true true sender512.pfx .//test//Zip8Test.zip"); 68 System.exit(-1); 69 } 70 71 String smtpHost = args[0]; 72 String addressTO = args[1]; 73 String cerFileName = args[2]; 74 String algorithmName = args[3]; 75 String digestAlgorithm = args[4]; 76 77 boolean includingCert = true; 78 79 if (args[5].equals("true")) 80 includingCert = true; 81 else 82 includingCert = false; 83 84 boolean includingSignAttrib = true; 85 86 if (args[6].equals("true")) 87 includingSignAttrib = true; 88 else 89 includingSignAttrib = false; 90 91 String pfxfileName = args[7]; 92 93 String fileName = null; 94 95 if (args.length > 8) 96 fileName = args[8]; 97 98 String addressCC = "recipient@seateam.co.yu"; 99 String addressBCC = "recipient@seateam.co.yu"; 100 101 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 102 args[6] + " " + args[7] + " " + subject; 103 104 File htmlContent = new File("./test/HtmlTest.html"); 105 106 SignedAndEnvelopedSMIME see = null; 107 108 try { 109 see = new SignedAndEnvelopedSMIME(smtpHost, from, subject); 110 111 see.setContent(htmlContent, "text/html"); 112 // To send messages content withouth resources from FileInputStream (or any 113 // other input stream) comment previous line and use following commented line 114 // see.setContent(new FileInputStream(htmlContent),"text/html"); 115 116 if (fileName != null) { 117 see.addAttachment(fileName); // optional - use this if send attachment 118 } 119 120 see.setReply(from); // optional 121 122 see.addRecipient(addressTO, "TO", cerFileName); // mandatory 123 // see.addRecipient(addressCC, "CC", cerFileName); // optional 124 // see.addRecipient(addressBCC, "BCC", cerFileName); // optional 125 126 see.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); // optional 127 see.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); // optional 128 see.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); // optional 129 130 see.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 131 132 if (algorithmName.equals("RC240")) { 133 System.out.println("Creating signed and encrypted message with RC2 - 40 bits algorithm... "); 134 see.signingAndEnveloping("SIGN_FIRST"); // instead of this next line could be used 135 // see.enveloping("RC2_CBC", 40, "SIGN_FIRST"); 136 } else if (algorithmName.equals("RC264")) { 137 System.out.println("Creating signed and encrypted message with RC2 - 64 bits algorithm... "); 138 see.signingAndEnveloping("RC2_CBC", 64, "SIGN_FIRST"); // send message with RC2 - 64 bits algorithm 139 } else if (algorithmName.equals("RC2128")) { 140 System.out.println("Creating signed and encrypted message with RC2 - 128 bits algorithm... "); 141 see.signingAndEnveloping("RC2_CBC", 128, "SIGN_FIRST"); // send message with RC2 - 128 bits algorithm 142 } else if (algorithmName.equals("DES")) { 143 System.out.println("Creating signed and encrypted message with DES algorithm... "); 144 see.signingAndEnveloping("DES", 56, "SIGN_FIRST"); // send message with DES - 56 bits algorithm 145 } else if (algorithmName.equals("3DES")) { 146 System.out.println("Creating signed and encrypted message with 3DES algorithm... "); 147 see.signingAndEnveloping("DES_EDE3_CBC", 192, "SIGN_FIRST"); // send message with 3DES - 192 bits algorithm 148 } 149 150 System.out.print("Sending signed and encrypted message ... "); 151 see.send(); // instead of this next line could be used 152 // Transport.send(see.getSignedMessage()); 153 System.out.println("done."); 154 155 } catch (Exception e) { 156 SMIMEException.setErrorFilePath("Log"); //specifies directory for logging 157 if (e instanceof SMIMEException) { 158 // ((SMIMEException)e).loggingErrors(null); //logging 159 ((SMIMEException) e).displayErrors(null); 160 } else 161 System.out.println(e.getMessage()); 162 } 163 } 164 }

This page was automatically generated by Maven